package com.mars.boss.weixin.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSON;
import com.mars.boss.weixin.entity.WeixinKeyWord;
import com.mars.boss.weixin.entity.WeixinMenu;
import com.mars.boss.weixin.entity.WeixinNewsArticle;
import com.mars.boss.weixin.service.WeixinKeyWordService;
import com.mars.boss.weixin.service.WeixinMenuService;
import com.mars.boss.weixin.service.WeixinNewsArticleService;
import com.mars.boss.weixin.service.WeixinService;
import com.mars.boss.weixin.wx.model.Article;
import com.mars.boss.weixin.wx.model.NewsMessage;

@Service
public class WeixinServiceImpl implements WeixinService {
	
	@Autowired
	private WeixinMenuService menuService ;
	
	@Autowired
	private WeixinNewsArticleService newsArticleService ;
	
	@Autowired
	private WeixinKeyWordService keyWordService ;

	@Override
	public String menuProcess(String key) {
		WeixinMenu menu = this.menuService.getMenuByKey(key) ;
		return JSON.toJSONString(menu);
	}
	
	/**
	 * 获取图文信息
	 * @param categoryId 图文类别ID
	 * @return
	 */
	public NewsMessage getNews(Long categoryId) {
		NewsMessage news = new NewsMessage() ;
		
		List<WeixinNewsArticle> articles = this.newsArticleService.getArticlesByCategoryId(categoryId) ;
		if(null != articles) {
			List<Article> articleList = new ArrayList<Article>() ;
			for (WeixinNewsArticle article : articles) {
				Article a = new Article() ;
				a.setTitle(article.getTitle());
				a.setDescription(article.getDescription()) ;
				a.setUrl(article.getUrl());
				a.setPicUrl(article.getPicUrl());
				
				articleList.add(a) ;
			}
			
			news.setArticleCount(articleList.size());
			news.setArticles(articleList);
		}
		
		return news;
	}

	/**
	 * 关键字查询
	 */
	@Override
	public WeixinKeyWord getKeyword(String keyword) {
		return this.keyWordService.getKeyword(keyword) ;
	}

}
